package com.wkbb.basic.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wkbb.basic.mapper.EmployeeMapper;
import com.wkbb.basic.mapper.OrgStructureUserMapper;
import com.wkbb.basic.mapper.StudentMapper;
import com.wkbb.basic.model.Employee;
import com.wkbb.basic.model.OrgStructureUser;
import com.wkbb.basic.model.Student;
import com.wkbb.basic.service.OrgStructureUserService;
import com.wkbb.common.base.BaseServiceImpl;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.EmployeeInfoDto;
import com.wkbb.common.dto.basic.StudentInfoDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author lizhichao
 * @date 2019/8/9
 * @description
 */
@Service
@Slf4j
public class OrgStructureUserServiceImpl extends BaseServiceImpl<OrgStructureUser> implements OrgStructureUserService {

    @Autowired
    OrgStructureUserMapper orgStructureUserMapper;

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    EmployeeMapper employeeMapper;

    @Autowired
    public void setMapper(OrgStructureUserMapper mapper) {
        super.setMapper(mapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultDTO addAlumnus(StudentInfoDto studentInfoDto, String userName, Long structureId, long orgId) throws Exception {

            if (studentInfoDto.getId() == null || studentInfoDto.getUserId() == null) {
                return new ResultDTO(ErrorCode.PARAM_MISSING);
            }

            Student student = new Student();
            student.setId(studentInfoDto.getId());
            student.setStatus(false);
            student.setUpdateBy(userName);
            //修改学生表状态为无效
            this.studentMapper.update(student);
            OrgStructureUser orgStructureUser = new OrgStructureUser();
            orgStructureUser.setUserId(studentInfoDto.getUserId());
            orgStructureUser.setStructureId(structureId);
            orgStructureUser.setOrgId(orgId);
            //增加组织用户表数据
            this.orgStructureUserMapper.insert(orgStructureUser);
        return new ResultDTO(true);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultDTO addAlumnusBatch(List<StudentInfoDto> studentInfoDtos, String userName, Long structureId, long orgId) throws Exception {

        List<OrgStructureUser> orgStructureUserList = new ArrayList();
        List<Student> studentList = new ArrayList();
        for(StudentInfoDto studentInfoDto : studentInfoDtos){
            if (studentInfoDto.getId() == null || studentInfoDto.getUserId() == null) {
                return new ResultDTO(ErrorCode.PARAM_MISSING);
            }

            Student student = new Student();
            student.setId(studentInfoDto.getId());
            student.setStatus(false);
            student.setUpdateBy(userName);
            studentList.add(student);
            OrgStructureUser orgStructureUser = new OrgStructureUser();
            orgStructureUser.setUserId(studentInfoDto.getUserId());
            orgStructureUser.setStructureId(structureId);
            orgStructureUser.setOrgId(orgId);
            orgStructureUserList.add(orgStructureUser);
        }
        //修改学生表状态为无效
        this.studentMapper.updateBatch(studentList);
        //增加组织用户表数据
        this.orgStructureUserMapper.insertBatch(orgStructureUserList);
        return new ResultDTO(true);
    }

    @Override
    public ResultDTO importAlumnusBatch(List<Student> students, List<OrgStructureUser> orgStructureUserList) throws Exception{
        //批量更新职工表状态
        this.studentMapper.updateBatch(students);
        //批量插入职工表
        this.orgStructureUserMapper.insertBatch(orgStructureUserList);
        return new ResultDTO(true);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultDTO alumnusToStudent(StudentInfoDto studentInfoDto, String userName) throws Exception {
        if (studentInfoDto.getId() == null || studentInfoDto.getUserId() == null) {
            return new ResultDTO(ErrorCode.PARAM_MISSING);
        }

        Student student = new Student();
        student.setId(studentInfoDto.getId());
        student.setStatus(true);
        student.setUpdateBy(userName);
        //修改学生表状态为有效
        this.studentMapper.update(student);
        //删除组织用户表数据
        this.orgStructureUserMapper.deleteByPrimaryKey(studentInfoDto.getStructureUserId());
        return new ResultDTO(true);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultDTO alumnusToStudentBatch(List<StudentInfoDto> studentInfoDtos, String userName) throws Exception {
        List<OrgStructureUser> orgStructureUserList = new ArrayList<OrgStructureUser>();
        List<Student> studentList = new ArrayList();
        for(StudentInfoDto studentInfoDto : studentInfoDtos){
            if (studentInfoDto.getId() == null || studentInfoDto.getUserId() == null) {
                return new ResultDTO(ErrorCode.PARAM_MISSING);
            }

            Student student = new Student();
            student.setId(studentInfoDto.getId());
            student.setStatus(true);
            student.setUpdateBy(userName);
            studentList.add(student);
            OrgStructureUser orgStructureUser = new OrgStructureUser();
            orgStructureUser.setUserId(studentInfoDto.getUserId());
            orgStructureUser.setId(studentInfoDto.getStructureUserId());
            orgStructureUserList.add(orgStructureUser);
        }
        //修改学生表状态为有效
        this.studentMapper.updateBatch(studentList);
        //删除组织用户表数据
        this.orgStructureUserMapper.deleteBatch(orgStructureUserList);
        return new ResultDTO(true);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultDTO addRetiredTeacher(EmployeeInfoDto employeeInfoDto, String userName, Long structureId, long orgId) throws Exception {
       if(employeeInfoDto.getUserId() == null || employeeInfoDto.getId() == null){
           return new ResultDTO(ErrorCode.PARAM_MISSING);
       }

       Employee employee = new Employee();
       employee.setId(employeeInfoDto.getId());
       employee.setStatus(false);
       employee.setUpdateBy(userName);
       //更新职工表状态
       this.employeeMapper.update(employee);
       OrgStructureUser orgStructureUser = new OrgStructureUser();
       orgStructureUser.setUserId(employeeInfoDto.getUserId());
       orgStructureUser.setOrgId(orgId);
       orgStructureUser.setStructureId(structureId);
       ///插入职工表
       this.orgStructureUserMapper.insert(orgStructureUser);
        return new ResultDTO(true);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultDTO addRetiredTeacherBatch(List<EmployeeInfoDto> employeeInfoDtos, String userName, Long structureId, long orgId) throws Exception {
        List<OrgStructureUser> orgStructureUserList = new ArrayList();
        List<Employee> employees = new ArrayList();
        for(EmployeeInfoDto employeeInfoDto : employeeInfoDtos){
            if (employeeInfoDto.getId() == null || employeeInfoDto.getUserId() == null) {
                return new ResultDTO(ErrorCode.PARAM_MISSING);
            }
            Employee employee = new Employee();
            employee.setId(employeeInfoDto.getId());
            employee.setStatus(false);
            employee.setUpdateBy(userName);
            employees.add(employee);
            OrgStructureUser orgStructureUser = new OrgStructureUser();
            orgStructureUser.setUserId(employeeInfoDto.getUserId());
            orgStructureUser.setStructureId(structureId);
            orgStructureUser.setOrgId(orgId);
            orgStructureUserList.add(orgStructureUser);
        }
        //批量更新职工表状态
        this.employeeMapper.updateBatch(employees);
        //批量插入职工表
        this.orgStructureUserMapper.insertBatch(orgStructureUserList);
        return new ResultDTO(true);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultDTO importRetiredTeacherBatch(List<Employee> employees,List<OrgStructureUser> orgStructureUserList) throws Exception {
        //批量更新职工表状态
        this.employeeMapper.updateBatch(employees);
        //批量插入职工表
        this.orgStructureUserMapper.insertBatch(orgStructureUserList);
        return new ResultDTO(true);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultDTO retiredTeacherToTeacher(EmployeeInfoDto employeeInfoDto, String userName) throws Exception {

        Employee employee = new Employee();
        employee.setId(employeeInfoDto.getId());
        employee.setStatus(true);
        employee.setUpdateBy(userName);
        //更新职工表状态
        this.employeeMapper.update(employee);

        //删除组织用户表数据
        this.orgStructureUserMapper.deleteByPrimaryKey(employeeInfoDto.getStructureUserId());
        return new ResultDTO(true);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultDTO retiredTeacherToTeacherBatch(List<EmployeeInfoDto> employeeInfoDtos, String userName) throws Exception {
        List<OrgStructureUser> orgStructureUserList = new ArrayList();
        List<Employee> employees = new ArrayList();
        for(EmployeeInfoDto employeeInfoDto : employeeInfoDtos){
            if (employeeInfoDto.getId() == null || employeeInfoDto.getUserId() == null) {
                return new ResultDTO(ErrorCode.PARAM_MISSING);
            }
            Employee employee = new Employee();
            employee.setId(employeeInfoDto.getId());
            employee.setStatus(true);
            employee.setUpdateBy(userName);
            employees.add(employee);

            OrgStructureUser orgStructureUser = new OrgStructureUser();
            orgStructureUser.setId(employeeInfoDto.getStructureUserId());
            orgStructureUser.setUserId(employeeInfoDto.getUserId());
            orgStructureUserList.add(orgStructureUser);
        }
        //批量更新职工表状态
        this.employeeMapper.updateBatch(employees);
        //批量删除组织用户表数据
        this.orgStructureUserMapper.deleteBatch(orgStructureUserList);
        return new ResultDTO(true);
    }

    @Override
    public PageInfo<StudentInfoDto> getAlumnusInfoPageList(Map<String, Object> params, Integer pageNo, Integer pageSize) throws Exception {
        if (pageNo != null && pageSize != null) {
            Page<StudentInfoDto> page = PageHelper.startPage(pageNo, pageSize, true);
            PageInfo<StudentInfoDto> pageInfo = new PageInfo(this.orgStructureUserMapper.getAlumnusInfoPageList(params));
            pageInfo.setTotal(page.getTotal());
            return pageInfo;
        } else {
            return null;
        }
    }

    @Override
    public PageInfo<EmployeeInfoDto> getRetiredTeachersInfoPageList(Map<String, Object> params, Integer pageNo, Integer pageSize) throws Exception {
        if (pageNo != null && pageSize != null) {
            Page<EmployeeInfoDto> page = PageHelper.startPage(pageNo, pageSize, true);
            PageInfo<EmployeeInfoDto> pageInfo = new PageInfo(this.orgStructureUserMapper.getRetiredTeachersInfoPageList(params));
            pageInfo.setTotal(page.getTotal());
            return pageInfo;
        } else {
            return null;
        }
    }

    @Override
    public void updateByPrimaryKeySelective(Map<String, Object> params)
    {
        orgStructureUserMapper.updateByPrimaryKeySelective(params);
    }
}